﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using Grundy.Administration.Web.Services;
using Grundy.Entity;
using Grundy.Common;

namespace Grundy.Administration.Web.Controllers
{
    [Authorize]
    public class ProductController : Controller
    {
        private ProductService _productService;
        private FeatureService _featureService;

        public ProductController()
        {   
            _productService = new ProductService();
            _featureService = new FeatureService(_productService.Db);
        }

        public ProductController(ProductService productService, FeatureService featureService)
        {
            _productService = productService;
            _featureService = featureService;
        }

        [HttpPost]
        public ActionResult GetProductsData(int page = 1, int rows = 0)
        {
            var totalRecords = _productService.GetProducts().Count();
            var Products = _productService.GetProducts(page, rows)
                           .Select(i => new { i.Id, i.Name, Features = i.Features.Select(j => j.Name) })
                           .ToList();

            var data = new
            {
                total = page,
                page = page,
                records = totalRecords,
                rows = Products.Select((i) => new { Id = i.Id, cell = new object[] { i.Id, i.Name, string.Join(", ", i.Features) }}).ToList()
            };

            return Json(data);
        }

        public ActionResult GetProducts()
        {
            var totalRecords = _productService.GetProducts().Count();
            ViewBag.IsEmpty = totalRecords == 0;
            return View();
        }

        [HttpPost]
        public ActionResult New(string name, string features)
        {
            features = features ?? string.Empty;
            var featuresList = features.Split(new[] {", "}, StringSplitOptions.RemoveEmptyEntries);
            var product = _productService.GetProduct(name);
            if (product == null)
            {
                // Optimize this query
                var loadedFeatures = _featureService.GetFeatures().ToList().Where(i => featuresList.ContainsIgnoreCase(i.Name));
                var created = _productService.CreateProduct(name.Trim(), loadedFeatures);
                return Json(new object[] { true, string.Empty, created.Id });
            }
            return Json(new object[] { false, "A product with the same name already exists.", Guid.Empty });
        }

        public ActionResult SearchProducts(string term)
        {   
            if (term.IsNullOrEmpty() || term.Length < 2)
                return Json(new object[] { }, JsonRequestBehavior.AllowGet);

            var matching = _productService.GetProducts().Where(i => i.Name.StartsWith(term))
                           .Select(i => i.Name);
            return Json(matching, JsonRequestBehavior.AllowGet);
        }
    }
}